package com.jsnj33.pdaproject.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsnj33.pdaproject.R;

import java.util.List;

/**
 * @author 33oa
 * @date 2018/11/29
 */
public abstract class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    private Context mContext;
    private List<String> list;

    public MainAdapter(Context context, List<String> list) {
        this.mContext = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_main, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tv.setText(list.get(position).toString());
        if (list.get(position).toString().equals("进货单")) {
            holder.ivIcon.setImageResource(R.drawable.ic_jinhuodan);
        } else if (list.get(position).toString().equals("销货单")) {
            holder.ivIcon.setImageResource(R.drawable.ic_xiaohuo);
        } else if (list.get(position).toString().equals("领料单")) {
            holder.ivIcon.setImageResource(R.drawable.ic_lingliaodan);
        } else if (list.get(position).toString().equals("缴货单")) {
            holder.ivIcon.setImageResource(R.drawable.ic_jiaohuo);
        } else if (list.get(position).toString().equals("报工单")) {
            holder.ivIcon.setImageResource(R.drawable.ic_baogongdan);
        } else if (list.get(position).toString().equals("工序查询")) {
            holder.ivIcon.setImageResource(R.drawable.ic_gongxun);
        } else if (list.get(position).toString().equals("送检单")) {
            holder.ivIcon.setImageResource(R.drawable.ic_songjiandan);
        } else if (list.get(position).toString().equals("原料调拨单")) {
            holder.ivIcon.setImageResource(R.drawable.ic_yuanliao);
        } else if (list.get(position).toString().equals("箱码绑定")) {
            holder.ivIcon.setImageResource(R.drawable.ic_bind);
        } else if (list.get(position).toString().equals("箱码查询")) {
            holder.ivIcon.setImageResource(R.drawable.ic_look);
        } else if (list.get(position).toString().equals("Z数据查询")) {
            holder.ivIcon.setImageResource(R.drawable.ic_look);
        }
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    protected abstract void clickItem(String accounts);

    public void setList(List list) {
        this.list = list;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv;
        ImageView ivIcon;

        ViewHolder(View itemView) {
            super(itemView);

            tv = itemView.findViewById(R.id.tv);
            ivIcon = itemView.findViewById(R.id.ivIcon);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickItem(list.get(getAdapterPosition()).toString());
                }
            });
        }
    }
}
